package com.fairytask.ui.item
{
	import com.fairytask.engine.components.clip.Clip;
	import com.fairytask.engine.components.text.Label;
	import com.fairytask.engine.define.ResConfig;
	import com.fairytask.resource.StaticIni;
	import com.fairytask.tools.MoneyTools;
	
	import flash.display.Sprite;
	
	/**
	 * 翻牌卡片
	 * @author xujh
	 * 
	 */
	public class CardItem extends Sprite
	{
		private var _itemMc	:Clip;	//物品
		
		private var _removeMc	:Clip;	//去除的卡片
		private var _maskMc	:Clip;	//卡片反面]
		private var _moneyTxt	:Label;	//奖励
		
		private var _isRemove	:Boolean = false;	//是否屏蔽
		private var _isMask	:Boolean = false;	//是否背面
		private var _isItem	:Boolean = false;	//是物品
		private var _moneyType	:int;	//货币类型
		private var _moneyVal	:int;	//货币数值
		private var _configID	:int;
		
		//物品
		public static const ITEM	:int = 0;
		//银两
		public static const SILVER	:int = 1;
		//灵
		public static const SPIRIT	:int = 2;
		//精力
		public static const ENERGY	:int = 3;
		//仙玉
		public static const GOLD	:int = 4; 
		
		public function CardItem()
		{
			super();
			init();
		}
		
		/**
		 * 初始化 
		 * 
		 */
		private function init():void
		{
		}
		
		/**
		 * 设置奖励 
		 * @param type	 0 物品奖励 1 银两  2 灵 3 精力 4仙玉
		 * @param itemID
		 * @param anoumt
		 * @return 
		 * 
		 */
		public function setData(type:int,itemID:int,anoumt:int):void
		{
			if(_isMask)
			{
				removeChild(_maskMc);
				_isMask = false;
			}
			_moneyType = type;
			switch(type)
			{
				case ITEM:		//物品
					addItem(itemID,anoumt);
					break;
				case SILVER:		//银两
				case SPIRIT:		//灵
				case ENERGY:		//精力
				case GOLD:		//仙玉
					addMoney(type,anoumt);
					break;
			}
		}
		
		/**
		 * 添加物品 
		 * 
		 */
		private function addItem(id:int,val:int):void
		{
			//初始化
			if(null == _itemMc)
			{
				_itemMc = new Clip();
				_itemMc.x = _itemMc.y = 2;
			}
			addChild(_itemMc);
			_itemMc.source = ResConfig.iconPath + StaticIni.getIniVar(id,"Icon") + ResConfig.iconImg;
			_isItem = true;
			_configID = id;
			setAmount(val);
		}
		
		/**
		 * 添加金币 
		 * @param type
		 * @param anoumt
		 * 
		 */
		private function addMoney(type:int,val:int):void
		{
			//初始化
			if(null == _itemMc)
			{
				_itemMc = new Clip();
				_itemMc.x = _itemMc.y = 2;
			}
			addChild(_itemMc);
			
			//内容
			switch(type)
			{
				case SILVER:		//银两
					_itemMc.source = ResConfig.pngResPath + "slive.jpg";
					break;
				case SPIRIT:		//灵
					_itemMc.source = ResConfig.pngResPath + "sprit.jpg";
					break;
				case ENERGY:		//精力
					_itemMc.source = ResConfig.pngResPath + "SP.jpg";
					break;
				case GOLD:		//仙玉
					_itemMc.source = ResConfig.pngResPath + "gold.jpg";
					break;
			}
			_moneyVal = val;
			setAmount(val);
			_isItem = false;
			_configID = 0;
		}
		
		/**
		 * 设置数量 
		 * @param val
		 * 
		 */
		private function setAmount(val:int):void
		{
			if(val > 1)
			{
				if(null == _moneyTxt)
				{
					_moneyTxt = new Label();
					_moneyTxt.y = 32;
				}
				addChild(_moneyTxt);
				_moneyTxt.htmlStrByBorder = MoneyTools.setMoney(val);
				_moneyTxt.x = 56 - _moneyTxt.width - 2;
			}
		}
		
		/**
		 * 是否屏蔽 
		 * @param val
		 * 
		 */
		public function set isRemove(val:Boolean):void
		{
			if(val)
			{
				if(null == _removeMc)
				{
					_removeMc = new Clip();
					_removeMc.source = ResConfig.pngResPath + "unUse.png";
				}
				addChild(_removeMc);
			}
			else
			{
				if(contains(_removeMc))
				{
					removeChild(_removeMc);
				}
			}
			_isRemove = val;
		}
		
		public function get isRemove():Boolean
		{
			return _isRemove;
		}
		
		/**
		 * 是否背面
		 * @return 
		 * 
		 */
		public function get isMask():Boolean
		{
			return _isMask;
		}
		
		public function get isItem():Boolean
		{
			return _isItem;
		}
		
		public function get configID():int
		{
			return _configID;
		}
		
		public function get moneyType():int
		{
			return _moneyType;
		}
		
		public function get moneyVal():int
		{
			return _moneyVal;
		}
		
		/**
		 * 牌翻过去 
		 * 
		 */
		public function maskCard():void
		{
			clearAll();
			if(null == _maskMc)
			{
				_maskMc = new Clip();
				_maskMc.source = ResConfig.pngResPath + "goodBG.png";
				_maskMc.width = _maskMc.height = 50;
				_maskMc.x = _maskMc.y = 3;
			}
			addChild(_maskMc);
			_isMask = true;
		}
		
		/**
		 * 清除所有
		 * 
		 */
		public function clearAll():void
		{
			while(numChildren>0)
			{
				removeChildAt(0);
			}
			_isRemove = false;
			_isMask = false;
			_isItem = false;
			_moneyType = 0;
			_moneyVal = 0;
		}
	}
}